package imvu.rbox.object
{
	import anshe.object.ExTextField;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import imvu.rbox.event.ActionEvent;
	import imvu.rbox.struct.CGameRoomInfo;

	public class RoomLine extends MovieClip
	{
		public var txtRoomName:ExTextField;		//	房间名（多为房间创建者的名称）
		public var txtRoomUsers:TextField;		//	房间人数列表（当前/上限）
		public var btnJoin:SimpleButton;		//	加入游戏按钮
		
		private var roomInfo:CGameRoomInfo;		//	游戏房间对象
		
		public function RoomLine()
		{			
			this.info = null;
			
			this.txtRoomName.color = 0x000000;
			this.txtRoomName.mouseEnabled = false;
			this.txtRoomUsers.mouseEnabled = false;			
			
			btnJoin.addEventListener(MouseEvent.CLICK, onJoinRoom);
		}
		
		public function set info(rInfo:CGameRoomInfo):void
		{
			this.roomInfo = rInfo;
			
			if(rInfo)
			{
				this.visible = true;
				
				this.txtRoomName.text = this.roomInfo.masterNick + "'s room";
				
				this.btnJoin.visible = !this.roomInfo.isPlaying;
				
				if(this.roomInfo.isPlaying)
				{				
					this.gotoAndStop(2);
					this.txtRoomUsers.text = "Playing...";
				}
				else
				{
					this.gotoAndStop(1);
					this.txtRoomUsers.text = "(" + this.roomInfo.nowPlayer + "/" + this.roomInfo.maxPlayer + ")";
				}
				
				if(rInfo.nowPlayer >= rInfo.maxPlayer || rInfo.isPlaying)
				{
					this.btnJoin.visible = false;
				}
				else
				{
					this.btnJoin.visible = true;
				}
			}
			else
			{
				this.visible = false;
				this.gotoAndStop(1);
			}
		}		
		
		public function get info():CGameRoomInfo
		{
			return this.roomInfo;
		}
		
		
		//----------------------------------------------------------------------
		
		private function onJoinRoom(e:MouseEvent):void
		{
			if(this.info)
			{
				this.dispatchEvent(new ActionEvent(ActionEvent.JOIN_ROOM, this.info));
			}			
		}
	}
}